<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加修改</title>
    <link rel="stylesheet" href="/res/layui/css/layui.css"  media="all">
    <script src="/res/layui/layui.js"></script>
    <script src="/res/js/tools.js"></script>
</head>
<body>
<form class="layui-form" lay-filter="example" id="myform" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">名称：</label>
        <div class="layui-input-block">
            <input type="hidden" name="coid">
            <input type="hidden" id="userid" name="userid">
            <input type="text" name="coname" required  lay-verify="required" placeholder="商品名称" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-inline">
            <input type="text" id="comoney" name="comoney" required lay-verify="required|number" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">图片</label>
        <div class="layui-input-inline">
            <input type="file" id="coFile" name="coFile" required lay-verify="required"  autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-inline">
            <input type="text" id="coremark" name="coremark" required lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script>
    var $ = layui.$;
    //Demo

    let user =JSON.parse(sessionStorage.getItem("user"));
    if (user==null){
        location.href="/pages/login.html";
    }
    $("#userid").val(user.userid);
    layui.use('form', function(){
        var form = layui.form
            ,layedit = layui.layedit;

        var layer = layui.layer //弹层
            ,laypage = layui.laypage //分页
            ,laydate = layui.laydate //日期
            ,table = layui.table //表格
            ,carousel = layui.carousel //轮播
            ,upload = layui.upload //上传
            ,element = layui.element //元素操作
            ,slider = layui.slider //滑块
            ,dropdown = layui.dropdown //下拉菜单


        //给表单赋值
        setFormValue();

        function setFormValue() {
            let datad = parent.commDate;
            console.info(datad);
            if (datad) { //flId==""  flId==null  flId==undefined
                console.info("进if");
                form.val('example',datad);

            }
        }
        //监听提交
        form.on('submit(formDemo)', function(data){
            //layer.msg(JSON.stringify(data.field));
            let formData = new FormData(document.getElementById("myform"));
            let url = "/sCommodity/addAndUpdate";

            $.ajax({
                async:true, //true异步.  false 同步
                type:"POST", //请求方式
                url:url,    //请求的路径
                data:formData,  //请求的参数
                contentType: false, //发送信息至服务器时内容编码类型
                processData: false, //通过data选项传递进来的数据
                dataType:"json", //返回类型
                success:function(res){ //成功回调函数
                    if(res.resp==1){
                        //在子页面里面调用父页面的方法
                        parent.closeLayer();
                    }else{
                        layer.msg("操作失败");
                    }
                },error:function(){ //失败的回调函数

                }
            });
            /*$.post(url,formData,function (res) {
                console.info(res);
                if (res==1){
                    console.info("进入if");
                    parent.closeLayer();
                }else {
                    layer.msg("操作失败");
                }
            })*/
            return false;
        },"json");

    });

</script>

</body>
</html>